#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "stm32f10x.h"

#include "public_lib.h"
#include "xpqueue.h"
#include "serial.h"
#include "system_timer.h"
#include "irda_task.h"

typedef struct
{
      char is_first_edge;
    s_timeval last_time;
      s_timeval time;
      int key;
}s_irda_task_info;

static s_irda_task_info info = {1, 0};

static pq_queue_t *key_queue = NULL;

void start_irda_task(void *args)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    EXTI_InitTypeDef EXTI_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG|RCC_APB2Periph_AFIO,ENABLE);

    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_15;
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
    GPIO_Init(GPIOG,&GPIO_InitStructure);
    
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOG, GPIO_PinSource15); 
    EXTI_ClearITPendingBit(EXTI_Line15);
    
    EXTI_InitStructure.EXTI_Line=EXTI_Line15;
    EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
    EXTI_InitStructure.EXTI_LineCmd=ENABLE;
    EXTI_Init(&EXTI_InitStructure); 

    NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;  
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;     
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
    NVIC_Init(&NVIC_InitStructure);     
    
    if(key_queue==NULL)
    {
        key_queue =  pq_create_queue();
    }   
}

int irda_task_run(void *args)
{
    
    
    return 0;
}

int irda_task_manage(int argc, char **argv, void *args)
{
    int err_code = -1;

    if(argc >= 2)
    {
       
        if(strcasecmp(argv[0], "infrared")==0)
        {
            if(strcasecmp(argv[1], "enable") == 0)
            {
                
            }
            else if(strcasecmp(argv[1], "auto_feed") == 0)
            {                
                if(argc == 3)
                {
                                  
                }
                else
                {
                    serial_writeline("%s %s usage:  %s %s enable[0/1]", argv[0], argv[1], argv[0], argv[1]);
                }
            }                        
        }        
        else{/* not for me, do nothing */}
    }
    else{/* not for me, do nothing */}

    return err_code;
}


void EXTI15_10_IRQHandler(void)      
{
     if(info.is_first_edge)
     {
           info.is_first_edge = 0;
           get_timer_time(&info.last_time);
     }
     else
     {
           double diff = 0;
           get_timer_time(&info.time);
           diff = get_diff_time(&info.last_time, &info.time);
           if(diff <= 1.3)
             {
                   serial_write("0");
             }
             else if(diff <= 2.3)
             {
                   serial_write("1");
             }
             else if(diff <= 4.3)
             {
                   serial_write("s");
             }
             else
             {
                   serial_write("f");
                   info.is_first_edge = 1;   
             }           
         
           info.last_time = info.time;
     }  

     EXTI_ClearITPendingBit(EXTI_Line15);    
}
